package com.eastfair.home.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.home.dao.ReportSiteAuditMapper;
import com.eastfair.home.dto.ReportSiteAuditDTO;
import com.eastfair.home.entity.ReportSiteAudit;
import com.eastfair.home.exceptioncode.HomeExceptionCode;
import com.eastfair.home.service.ReportSiteAuditService;
import com.eastfair.home.vo.ReportSiteAuditVO;
import com.eastfair.venueservice.consts.CommonConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 报馆展位基本信息审核记录
 * </p>
 *
 * @author dq
 * @date 2022-08-08
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class ReportSiteAuditServiceImpl extends SuperServiceImpl<ReportSiteAuditMapper, ReportSiteAudit> implements ReportSiteAuditService {

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<ReportSiteAudit> modelList) {
        modelList.forEach(s -> s.setIsDeleted(BusinessConstant.YES));
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(ReportSiteAudit model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    @Override
    public ReportSiteAuditVO getVOByReportSpaceAuditId(Long reportSpaceAuditId) {
        log.info("getReportSiteVOByReportSpaceId - 报馆审核ID查询展位信息审核, reportSpaceAuditId={}", reportSpaceAuditId);
        ReportSiteAudit reportSiteAudit = getByReportSpaceAuditId(reportSpaceAuditId);
        if (reportSiteAudit == null) {
            return null;
        }
        return BeanUtil.toBean(reportSiteAudit, ReportSiteAuditVO.class);
    }

    @Override
    public ReportSiteAudit getByReportSpaceAuditId(Long reportSpaceAuditId) {
        ReportSiteAuditDTO query = new ReportSiteAuditDTO();
        query.setReportSpaceAuditId(reportSpaceAuditId);
        return getReportSiteAudit(query);
    }

    @Override
    public ReportSiteAudit getByReportSpaceId(Long reportSpaceId) {
        ReportSiteAuditDTO query = new ReportSiteAuditDTO();
        query.setReportSpaceId(reportSpaceId);
        return getReportSiteAudit(query);
    }

    @Override
    public ReportSiteAudit getReportSiteAudit(ReportSiteAuditDTO query) {
        log.info("getReportSiteAudit - 获取展位信息审核, query={}", query);
        QueryWrapper<ReportSiteAudit> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .lambda()
                .last("limit 1")
                .eq(ReportSiteAudit::getIsDeleted, BusinessConstant.DELETE_NO)
                .eq(query.getReportSpaceAuditId() != null, ReportSiteAudit::getReportSpaceAuditId, query.getReportSpaceAuditId())
                .eq(query.getReportSpaceId() != null, ReportSiteAudit::getReportSpaceId, query.getReportSpaceId())
        ;
        return getOne(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean submitReportSiteAudit(ReportSiteAuditDTO reportSiteAuditDTO) {
        removeByReportSpaceId(reportSiteAuditDTO.getReportSpaceId());
        return saveReportSiteAudit(reportSiteAuditDTO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveReportSiteAudit(ReportSiteAuditDTO reportSiteAuditDTO) {
        ReportSiteAudit reportSiteAudit = BeanUtil.toBean(reportSiteAuditDTO, ReportSiteAudit.class);
        return save(reportSiteAudit);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateByReportSpaceAuditId(ReportSiteAuditDTO reportSiteAuditDTO, Long reportSpaceAuditId) {
        reportSiteAuditDTO.setReportSpaceAuditId(reportSpaceAuditId);
        ReportSiteAudit reportSiteAudit = getByReportSpaceAuditId(reportSpaceAuditId);
        if (reportSiteAudit == null) {
            throw BizException.wrap(HomeExceptionCode.REPORT_SITE_AUDIT_NOT_EXIST);
        }
        CopyOptions copyOptions = CopyOptions
                .create()
                .setIgnoreProperties(CommonConstants.COMMON_IGNORE_PROPERTIES);
        BeanUtil.copyProperties(reportSiteAuditDTO, reportSiteAudit, copyOptions);
        return updateAllById(reportSiteAudit);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByReportSpaceId(Long reportSpaceId) {
        ReportSiteAudit reportSiteAudit = getByReportSpaceId(reportSpaceId);
        if (reportSiteAudit == null) {
            return false;
        }
        List<ReportSiteAudit> list = new ArrayList<>();
        list.add(reportSiteAudit);
        return removeByIdsOfLogic(list);
    }
}
